using UnityEngine;
using System.Collections;

/**
 * Script of time display behaviour description.
 * The purpose of this class is to display the time.
 * @author Benjamin Bruneau
 */
public class DisplayTimeScript : MonoBehaviour 
{
	
	private const int TIMEMAXLENGTH = 11; //Time string maxlength
	private const string REPLAY_MODE_NAME = "ReplayMode"; // Replay Mode GameObject's name
	
	private GameObject _replayMode; // Replay mode GameObject
	
	public GameObject _dataContainer; // Data container GameObject
	
	/**
	 * Called on initialization.
	 * @return void
	 */
	void Start () 
	{

		//Initialized because these objects moves through scenes
		_replayMode = GameObject.Find (REPLAY_MODE_NAME);
		
	}
	
	/*
	 * Called once per frame.
	 * @return void
	 */
	void Update ()
		
	{
		// If not in replay mode
		if(_replayMode != null){
			if(!_replayMode.GetComponent<ReplayModeScript>()._replayMode)
				{
				//Get current timer
				float time = _dataContainer.GetComponent<TimeScript>().getTime();
				
				//Format it
				string tempTimeString = System.TimeSpan.FromSeconds(time).ToString();
				string timeString;
				if (tempTimeString.Length < TIMEMAXLENGTH)
				{
					timeString = tempTimeString.Substring(3);
				} else {
					timeString = tempTimeString.Substring(3,8);
				}
				
				//Set timer to display
				this.gameObject.GetComponent<TextMesh>().text = timeString;
			}
		}
	}
}
